package cc.yugu.yhdw.webserver.web.controller.security.hdw;


import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.hdw.Rescuer;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.common.utils.HttpClientUtils;
import cc.yugu.yhdw.common.utils.IdUtils;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.hdw.RescuerService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/security/hdw/rescuer")
public class RescuerController extends SecurityController {
    @Autowired
    RescuerService rescuerService;


    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute("starEnum", Rescuer.Star.values());
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_01_05.getValue());
    }

    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(Rescuer search, HttpServletRequest request) {
//        if (getSessionUser(request).getType()== User.Type.SUPPLIER.getValue()){
//            return null;
//        }
        if (getSessionUser(request).getType() == User.Type.AGENT.getValue()||getSessionUser(request).getType() == User.Type.AGENCY.getValue()) {
            search.setAgentId(getSessionUser(request).getAgentId());
        }
        return PageResult.successResult(rescuerService.findPage(search));
    }

    @RequestMapping(value = "add.htm")
    public void add(Model model) {
        model.addAttribute("starEnum", Rescuer.Star.values());
    }

    @RequestMapping("create.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult create(Rescuer rescuer) {
        return rescuerService.create(rescuer);
    }

    @RequestMapping(value = "image.htm", method = RequestMethod.GET)
    public void portrait() {
    }

    @RequestMapping(value = "image.htm", method = RequestMethod.POST)
    public String portrait( @RequestParam("file")MultipartFile file, Model model) throws IOException {
        File sourceFile = new File(getAppConfig().tempDir, IdUtils.uuid() + "." + YhdwUtils.getFileSuffix(file.getOriginalFilename()));
        YhdwUtils.makeParentDir(sourceFile);
        try {
            file.transferTo(sourceFile);
        } catch (IOException e) {
            model.addAttribute("message", "上传web服务器失败");
            return SEGMENT_MESSAGE;
        }
        Map<String,File> fileMap = new HashMap<String, File>();
        fileMap.put("file",sourceFile);
        Map<String,String> paramMap = new HashMap<String, String>();

        HttpClientUtils.HttpResp httpResp;
        httpResp = HttpClientUtils.uploadFile(appConfig.staticUrl + "/security/hdw_upload/rescuer_image.htm",fileMap,paramMap,YhdwUtils.buildHttpHeader(System.currentTimeMillis(),appConfig.uploadSalt));

        if(httpResp.status / 100 != 2) {
            model.addAttribute("message","上传静态服务器失败");
            return SEGMENT_MESSAGE;
        }
        Map map = (Map) YhdwUtils.decodeJson(httpResp.content, Map.class);
        Map data = (Map) map.get("data");

        model.addAttribute("filePath",data.get("filePath"));
        model.addAttribute("staticUrl", appConfig.getStaticUrl());
        model.addAttribute("fileName", data.get("fileName"));
        return "/security/hdw/rescuer/image_response";
    }


    @RequestMapping(value = "logo.htm", method = RequestMethod.GET)
    public void portraitLogo() {

    }

    @RequestMapping(value = "logo.htm", method = RequestMethod.POST)
    public String portraitLogo( @RequestParam("file")MultipartFile file, Model model) throws IOException {
        File sourceFile = new File(getAppConfig().tempDir, IdUtils.uuid() + "." + YhdwUtils.getFileSuffix(file.getOriginalFilename()));
        YhdwUtils.makeParentDir(sourceFile);
        try {
            file.transferTo(sourceFile);
        } catch (IOException e) {
            model.addAttribute("message", "上传web服务器失败");
            return SEGMENT_MESSAGE;
        }
        Map<String,File> fileMap = new HashMap<String, File>();
        fileMap.put("file",sourceFile);
        Map<String,String> paramMap = new HashMap<String, String>();

        HttpClientUtils.HttpResp httpResp;
        httpResp = HttpClientUtils.uploadFile(appConfig.staticUrl + "/security/upload/shop_logo.htm",fileMap,paramMap,YhdwUtils.buildHttpHeader(System.currentTimeMillis(),appConfig.uploadSalt));

        if(httpResp.status / 100 != 2) {
            model.addAttribute("message","上传静态服务器失败");
            return SEGMENT_MESSAGE;
        }
        Map map = (Map) YhdwUtils.decodeJson(httpResp.content, Map.class);
        Map data = (Map) map.get("data");

        model.addAttribute("filePath",data.get("filePath"));
        model.addAttribute("staticUrl", appConfig.getStaticUrl());
        model.addAttribute("fileName", data.get("fileName"));
        return "/security/hdw/rescuer/logo_response";
    }


    @RequestMapping(value = "view.htm")
    public String view(Model model, int id) {
        model.addAttribute("id", id);
        return "/security/hdw/rescuer/view";
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "view_basic.htm")
    public String viewBasicInfo(Model model, int id) {
        Rescuer entity = rescuerService.find(id);
        for (Rescuer.Star star : Rescuer.Star.values()){
            if (entity.getStar()==star.getValue()){
                entity.setStarName(star.getName());
                break;
            }
        }
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/hdw/rescuer/view_basic";
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "view_map.htm")
    public String viewMap(Model model, int id) {
        Rescuer entity = rescuerService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/hdw/rescuer/view_map";
    }
    @RequestMapping(value = "edit.htm")
    public String edit(Model model, int id) {
        Rescuer entity = rescuerService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        model.addAttribute("starEnum", Rescuer.Star.values());
        return "/security/hdw/rescuer/edit";
    }

    @RequestMapping("update.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult update(Rescuer entity) {
        return rescuerService.update(entity);
    }

    @RequestMapping("delete.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult delete(int id) {
        return rescuerService.delete(id);
    }

}
